<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="${webcontext}/res/style/S.css" />
<script type="text/javascript" src="${webcontext}/res/js/jquery.min.js"></script>
<script type="text/javascript" src="${webcontext}/res/js/S.js"></script>
<script type="text/javascript">
	$(function(){
		
		//configs
		$('#addconfig').click(function(){
			var dialog = new S.Dialog({
				title:'增加抓取配置',
				width:500,
				height:300,
				modal:true,
				url:'${webcontext}/${pathvar.admin}/ds/spider/config/edit',
				buttons:{
					'保存':function(){
						if(!this.form.validate()){new S.Info('表单填写不完整','warn').show();return;}
						S.request({
							url:'${webcontext}/${pathvar.admin}/ds/spider/config/save',
							data:this.form.param(),
							success:function(){
								new S.Info('操作成功','success').show();
								cfgtree.reload();
								dialog.remove();
							}
						});
					},
					'取消':function(){
						this.remove();
					}
				}
			});
			dialog.show();
			//
		});
		$('#editconfig').click(function(){
			var items = cfgtree.getSelectedItems();
			if(items.length == 0){new S.Info('请选择配置','warn').show();return;}
			var dlg = new S.Dialog({
				title:'修改抓取配置',
				width:500,
				height:300,
				modal:true,
				url:'${webcontext}/${pathvar.admin}/ds/spider/config/edit?id='+items[0].data.id,
				buttons:{
					'修改':function(){
						if(!this.form.validate()){new S.Info('表单填写不完整','warn').show();return;}
						S.request({
							url:'${webcontext}/${pathvar.admin}/ds/spider/config/update',
							data:this.form.param(),
							success:function(){
								new S.Info('操作成功','success').show();
								cfgtree.reload();
								dlg.remove();
							}
						});
					},
					'取消':function(){
						this.remove();
					}
				}
			});
			dlg.show();
		});
		$('#deleteconfig').click(function(){
			var items = cfgtree.getSelectedItems();
			if(items.length == 0){new S.Info('请选择配置','warn').show();return;}
			S.confirm('警告','确认要删除选中的配置?',function(){
				var ids = [];
				S.each(items,function(i,node){
					ids.push(node.data.id);
				});
				S.request({
					url:'${webcontext}/${pathvar.admin}/ds/spider/config/delete',
					data:{ids:ids.join(',')},
					success:function(){
						new S.Info('操作成功','success').show();
						cfgtree.reload();
					}
				});
			},'warn');
		});
		//配置tree
		var cfgtree = new S.Tree({
			el:'#crawlerConfigs',
			ds:'${webcontext}/${pathvar.admin}/ds/spider/config/list',
			render:function(data){
				return '<i class="icons cog"></i>'+data.name;
			},
			click:function(node){
				$('#configId').val(node.data.id);
				pageRuleTbl.reload();
				extractRuleTbl.reload();
			}
		});
		//配置
		var pageRuleTbl = new S.Table({
			el:'#tbl1',
			pager:true,
			pageSize:30,
			scroll:true,
			dbclick:function(data,index){
				if(data== null){return;}
				editPageRule(data);
			}
		});
		$('#addPageRule').click(function(){
			
			var cfgId = $('#configId').val()
			if(cfgId == ''){new S.Info('请先选择抓取配置','warn').show();return;}
			
			var dlg = new S.Dialog({
				title:'增加路径规则',
				width:500,
				height:200,
				modal:true,
				url:'${webcontext}/${pathvar.admin}/ds/spider/pagerule/edit?configId='+cfgId,
				buttons:{
					'保存':function(){
						if(!this.form.validate()){new S.Info('填写有误或不完整','warn').show();return;}
						S.request({
							url:'${webcontext}/${pathvar.admin}/ds/spider/pagerule/save',
							data:S.fixConfig(this.form.param(),{configId:cfgId}),
							success: function(){
								new S.Info('操作成功','success').show();
								dlg.remove();
								pageRuleTbl.reload();
							}
						});
					},
					'关闭':function(){
						this.remove();
					}
				}
			});
			dlg.show();
			
		});
		
		function editPageRule(data){
			
			var dlg = new S.Dialog({
				title:'编辑路径规则',
				width:500,
				height:200,
				url:'${webcontext}/${pathvar.admin}/ds/spider/pagerule/edit?id='+data.id,
				modal:true,
				buttons:{
					'保存':function(){
						if(!this.form.validate()){new S.Info('填写不正确','warn').show();return;}
						S.request({
							url:'${webcontext}/${pathvar.admin}/ds/spider/pagerule/update',
							data:S.fixConfig(this.form.param(),{configId:$('#configId').val()}),
							success: function(){
								new S.Info('操作成功','success').show();
								pageRuleTbl.reload();
								dlg.remove();
							}
						});
					},
					'关闭': function(){
						this.remove();
					}
				}
			});
			dlg.show();
		}
		
		$('#editPageRule').click(function(){
			var items = pageRuleTbl.getCheckedRow();
			if(items.length!=1){
				new S.Infow('请选择一行','warn').show();return;
			}
			editPageRule(items[0]);
		});
		
		$('#deletePageRule').click(function(){
			
			var items = pageRuleTbl.getCheckedRow();
			if(items.length == 0){new S.Info().show();return;}
			
			S.confirm('警告','确认要删除选中的规则吗?',function(){
				var ids = [];
				S.each(items,function(i,item){
					ids.push(item.id);
				});
				S.request({
					url:'${webcontext}/${pathvar.admin}/ds/spider/pagerule/delete',
					data:{ids:ids.join(',')},
					success:function(){
						new S.Info('操作成功','success').show();
						pageRuleTbl.reload();
					}
				});
			},'danger');
		});
		
		//解析规则
		var extractRuleTbl = new S.Table({
			el:'#tbl2',
			pager:true,
			scroll:true,
			pageSize:10,
			renders:{
				type:function(v,data){
					if(v == 1){return '正则';}
					if(v == 2){return 'jsoup';}
				}
			},
			dbclick:function(data,index){
				if(data == null){return;}
				editExtractRule(data);
			}
		});
		
		$('#addExtractRule').click(function(){
			
			var configId = $('#configId').val()
			if(configId == ''){new S.Info('请先选择抓取配置','warn').show();return;}
			
			var dlg = new S.Dialog({
				title:'增加解析规则',
				width:500,
				height:300,
				modal:true,
				url:'${webcontext}/${pathvar.admin}/ds/spider/extractrule/edit?configId='+configId,
				buttons:{
					'保存':function(){
						if(!this.form.validate()){new S.Info('填写有误或不完整','warn').show();return;}
						S.request({
							url:'${webcontext}/${pathvar.admin}/ds/spider/extractrule/save',
							data:S.fixConfig(this.form.param(),{configId:configId}),
							success:function(){
								new S.Info('操作成功','success').show();	
								extractRuleTbl.reload();
								dlg.remove();
							}
						});
						
					},
					'关闭':function(){
						this.remove();
					}
				}
			});
			dlg.show();
			
		});
		$('#editExtractRule').click(function(){
			
			var items = extractRuleTbl.getCheckedRow();
			if(items.length !=1){
				new S.Info('请选择规则','warn').show();return;
			}
			
			editExtractRule(items[0]);
		});
		
		function editExtractRule(data){
			var dlg = new S.Dialog({
				title:'编辑解析规则',
				width:500,
				height:300,
				modal:true,
				url:'${webcontext}/${pathvar.admin}/ds/spider/extractrule/edit?id='+data.id,
				buttons:{
					'保存':function(){
						if(!this.form.validate()){new S.Info('填写有误或不完整','warn').show();return;}
						S.request({
							url:'${webcontext}/${pathvar.admin}/ds/spider/extractrule/update',
							data:this.form.param(),
							success:function(){
								new S.Info('操作成功','success').show();	
								extractRuleTbl.reload();
								dlg.remove();		
							}
						});
					},
					'关闭':function(){
						this.remove();
					}
				}
			});
			dlg.show();
		}
		
		
		$('#deleteExtractRule').click(function(){
			
			var items = extractRuleTbl.getCheckedRow();
			if(items.length ==0){
				new S.Info('请选择规则','warn').show();return;
			}
			S.confirm('警告','确认要删除选中规则?',function(){
				var ids= [];
				S.each(items,function(i,item){
					ids.push(item.id);
				});
				S.request({
					url:'${webcontext}/${pathvar.admin}/ds/spider/extractrule/delete',
					data:{ids:ids.join(',')},
					success:function(){
						new S.Info('操作成功','success').show();	
						extractRuleTbl.reload();	
					}
				});
			},'danger');
		});
		
		//测试解析按钮,大于50个字符的显示预览按钮，
		$('#btn_test_parse').click(function(){
			//
			
			var configId = $('#configId').val();
			if(configId == ''){new S.Info('请选择规则','warn').show();return;}
			
			var url = $('#txt_test_url').val();
			if(url == null || url.indexOf('http')!=0){
				new S.Info('请输入正确的URL地址','warn').show();
				return;
			}
			var renders = {
					value:function(str,data,key){
						if(str.length>50){
							return '<textarea>'+str+'</textarea><span class="btn"><i class="icons monitor_wallpaper"></i> 预览</span>'
						}
						return '<input type="text" value="'+str+'" />';
					}
				};
			S.request({
				modal:true,
				url:'${webcontext}/${pathvar.admin}/ds/spider/parse',
				data:{url:url,configId:$('#configId').val()},
				success:function(data){
					var html = [];
					for(var k in data){
						html.push(reTemplate.apply({key:k,value:data[k]},renders));
					}
					$('#tbl_result').html(html.join(''));
				}
			});
			$('#tbl_result').delegate('.btn','click',function(){
				var win = window.open('');
				win.document.write($(this).prev('textarea').val());
			});
		});
		var reTemplate = new S.Template('<tr><td style="width:50px;">@{key}</td><td>@{value}</td></tr>');
		
		
	});
</script>
</head>
<body>
<div class="filled-box">
	<div class="sidebar" style="width:300px;">
		<div class="toolbar">
				<span class="btn" id="addconfig"><i class="icons plus"></i> 增加</span>
				<span class="btn" id="editconfig"><i class="icons pencil"></i> 编辑</span>
				<span class="btn" id="deleteconfig"><i class="icons cross"></i> 删除</span>
		</div>
		<div class="content" id="crawlerConfigs">
			<div><i class="icons computer"></i> 配置列表</div>
		</div>
	</div>
	<div class="rightside" style="margin-left:300px;">
			<form name="cmmfrm" style="margin-bottom: 0px;">
				<input type="hidden" name="configId" id="configId" />
			</form>
		<div>
			<div style="display: inline-block;width: 45%;">
				<div class="toolbar">
				<span class="btn">爬取路径规则</span>
				<span class="btn" id="addPageRule"><i class="icons plus"></i> 增加</span>
				<span class="btn" id="editPageRule"><i class="icons pencil"></i> 编辑</span>
				<span class="btn" id="deletePageRule"><i class="icons cross"></i> 删除</span>
				</div>
				<table id="tbl1" form="cmmfrm" height="200" ds="${webcontext }/${pathvar.admin}/ds/spider/pagerule/list">
					<tr >
						<td width="30" seq></td>
						<td width="30" checkall></td>
						<td >URL Pattern</td>
						<td width="50">解析</td>
					</tr>
					<tr>
						<td>@{seq}</td>
						<td><input type="checkbox" value="@{id}" name="id"/></td>
						<td>@{pattern}</td>
						<td>@{parse}</td>
					</tr>
				</table>
			</div>
			<div style="display: inline-block;width: 50%;">
				<div class="toolbar">
					<span class="btn">页面解析规则</span>
					<span class="btn" id="addExtractRule"><i class="icons plus"></i> 增加</span>
					<span class="btn" id="editExtractRule"><i class="icons pencil"></i> 编辑</span>
					<span class="btn" id="deleteExtractRule"><i class="icons cross"></i> 删除</span>
				</div>
				<table id="tbl2" form="cmmfrm" height="200" ds="${webcontext }/${pathvar.admin}/ds/spider/extractrule/list">
					<tr >
						<td width="30" seq></td>
						<td width="30" checkall></td>
						<td width="80">字段</td>
						<td>规则</td>
						<td width="50">类型</td>
					</tr>
					<tr>
						<td>@{seq}</td>
						<td><input type="checkbox" value="@{id}" name="id"/></td>
						<td>@{name}</td>
						<td>@{rule}</td>
						<td>@{type}</td>
					</tr>
				</table>
			</div>
		</div>
		<div class="toolbar" style="margin-top: 10px; width: 95%;"><span class="btn">规则测试</span></div>
			<div class="content">
			<form id="xx">
				<div style="margin-bottom: 10px;">
				URL <input type="text" class="huge" id="txt_test_url" /> <span class="btn" id="btn_test_parse"><i class="icons auction_hammer_gavel"></i> 测试</span></div>
				<div>
				<table width="100%"  id="tbl_result"></table>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>